package test3;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Aug 1, 2007
 * Time: 7:46:42 PM
 * Revision: 0
 */
public class Test
{
    String v = "" +
            "public class gigel extends alabala implements test " +
            "{" +
            "}";
    Tokenizator parser = new Tokenizator(v.getBytes());

    String packageName = null;

    public Test()
    {
        parseProgram();
    }

    void parseProgram()
    {
/*        String token;

        token = parser.getToken();
        if (token.equals("package"))
            parsePackage();

        while ((token = parser.getToken()).length() > 0)
        {
            if (token.equals("import"))
                parseImport();
            else
                parseClass();
        }*/

        parseClass();
    }


    ClassDefinition parseClass()
    {
        ClassDefinition cd = new ClassDefinition();

        String token = parser.getToken();

        if (token.equals("public"))
        {
            cd.visibility = ClassDefinition.CLASS_VISIBILITY_PUBLIC;
            token = parser.getToken();
        }
        else
            if (token.equals("private"))
            {
                cd.visibility = ClassDefinition.CLASS_VISIBILITY_PRIVATE;
                token = parser.getToken();
            }

        if (token.equals("abstract"))
        {
            cd.isAbstract = true;
            token = parser.getToken();
        }

        if (token.equals("class"))
            parseClassDefinition(cd, false);
        else
            if (token.equals("interface"))
                parseClassDefinition(cd, true);
            else
            {
                System.out.println("ERROR: expecting class or interface");
            }

        return cd;
    }

    void parseClassDefinition(ClassDefinition cd, boolean isInteface)
    {
        cd.isInterface = isInteface;

        String token = parser.getToken();
        cd.name = token;

        token = parser.peekToken();
        if (token.equals("extends"))
        {
            parser.getToken();
            token = parser.getToken();

            cd.setExtends(token);


            if (parser.peekChar() == ',')
            {
                System.out.println("ERROR: can't extend more than one class");

                parser.getChar();
                token = parser.getToken();

                while (parser.peekChar() == ',')
                {
                    parser.getChar();
                    token = parser.getToken();
                }
            }

            token = parser.peekToken();
        }

        if (token.equals("implements"))
        {
            if (isInteface)
            {
                System.out.println("ERROR: interfaces can't implement other interfaces");
            }

            parser.getToken();

            token = parser.getToken();
            cd.addImplements(token);

            while (parser.peekChar() == ',')
            {
                parser.getChar();

                token = parser.getToken();
                cd.addImplements(token);
            }
        }

        if (parser.getChar() != '{')
        {
            System.out.println("ERROR: expecting {");
        }
    }

    boolean isModifier(String s)
    {
        return (s.equals("public") || s.equals("private") || s.equals("protected"));
    }

    void parseImport()
    {

    }

    void parsePackage()
    {
        if (packageName != null)
        {
            System.out.println("ERROR: package already defined");
            parser.consumeTo(';');

        }

        packageName = parser.getToken();

        while (parser.peekChar() == '.')
        {
            parser.getChar();
            packageName += '.' + parser.getToken();
        }

        if (parser.peekChar() != ';')
        {
            System.out.println("ERROR: expecting ; after package definition");
            parser.consumeTo('\n');
        }
        else
            parser.getChar();
    }


    public static void main(String[] args)
    {
        new Test();
    }
}
